#include<iostream>
using namespace std;
#include<cmath>
#include"library.h"

int main(){

	int n;
	double x,y,dy;

	double *x_data = new double [n];
	double *y_data = new double [n];

	FILE *fp=fopen("Nevill_input.txt","r");
	if(fp==NULL){
		cout<<"error: failed to open Nevill_input.txt.\n";
		exit(1);
	}

	n=0;
	while( fscanf(fp, "%*c%*[^\n]") != EOF ){
		n++;
	}
	cout<<"n="<<n<<endl;
	
	fp=fopen("Nevill_input.txt","r");
	for(int i=0;i<n;i++){
		fscanf(fp, "%lf %lf", &x_data[i], &y_data[i]);
	}
	fclose(fp);

	double xmin = x_data[0], xmax = x_data[0];
	for(int i=0;i<n;i++){
		if( xmin > x_data[i] ) xmin = x_data[i];
		if( xmax < x_data[i] ) xmax = x_data[i];
	}
	xmin -= 2;
	xmax += 2;

	int num_grid = 100;
	double step = (xmax - xmin)/num_grid;

	fp=fopen("Nevill_curve.txt","w");
	if(fp==NULL){
		cout<<"error: failed to open Nevill_curve.txt.\n";
		exit(1);
	}

	for(int i=0;i<num_grid;i++){
		x = xmin + step*i;
		Nevill(n,x_data,y_data,x, y, dy);
		fprintf(fp,"%lf   %lf   %lf \n", x, y, dy);
	}
	fclose(fp);

	return 0;
}

